﻿using System.Collections.Generic;
using facebook.Schema;

namespace facebook
{
	/// <summary>
	/// Facebook application api methods.
	/// </summary>
	public class comments
	{
		private readonly API _api;
		/// <summary>
		/// Public constructor for facebook.comments
		/// </summary>
		/// <param name="parent">Needs a connected API object for making requests</param>
		public comments(API parent)
		{
			_api = parent;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
        public IList<comment> get(string xid)
		{
			var parameterList = new Dictionary<string, string> { { "method", "facebook.comments.get" } };
            _api.AddRequiredParameter(parameterList, "xid", xid);

			var response = _api.SendRequest(parameterList);
			return !string.IsNullOrEmpty(response) ? comments_get_response.Parse(response).comment : null;
		}
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int add(string xid, string text)
        {
            var parameterList = new Dictionary<string, string> { { "method", "facebook.comments.add" } };
            _api.AddRequiredParameter(parameterList, "xid", xid);
            _api.AddRequiredParameter(parameterList, "text", text);

            var response = _api.SendRequest(parameterList);
            return !string.IsNullOrEmpty(response) ? comments_add_response.Parse(response).TypedValue : 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int add(string xid, string text, long uid)
        {
            var parameterList = new Dictionary<string, string> { { "method", "facebook.comments.add" } };
            _api.AddRequiredParameter(parameterList, "xid", xid);
            _api.AddRequiredParameter(parameterList, "text", text);
            _api.AddOptionalParameter(parameterList, "uid", uid);

            var response = _api.SendRequest(parameterList, false);
            return !string.IsNullOrEmpty(response) ? comments_add_response.Parse(response).TypedValue : 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int add(string xid, string text, long uid, string title, string url, bool publish_to_stream)
        {
            var parameterList = new Dictionary<string, string> { { "method", "facebook.comments.add" } };
            _api.AddRequiredParameter(parameterList, "xid", xid);
            _api.AddRequiredParameter(parameterList, "text", text);
            _api.AddOptionalParameter(parameterList, "uid", uid);
            _api.AddOptionalParameter(parameterList, "title", title);
            _api.AddOptionalParameter(parameterList, "url", url);
            if (publish_to_stream)
            {
                _api.AddOptionalParameter(parameterList, "publish_to_stream", publish_to_stream.ToString());
            }

            var response = _api.SendRequest(parameterList, false);
            return !string.IsNullOrEmpty(response) ? comments_add_response.Parse(response).TypedValue : 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool remove(string xid, int comment_id)
        {
            return remove(xid, comment_id, true);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool remove(string xid, int comment_id, bool useSession)
        {
            var parameterList = new Dictionary<string, string> { { "method", "facebook.comments.remove" } };
            _api.AddRequiredParameter(parameterList, "xid", xid);
            _api.AddRequiredParameter(parameterList, "comment_id", comment_id);

            var response = _api.SendRequest(parameterList, useSession);
            return string.IsNullOrEmpty(response) || comments_remove_response.Parse(response).TypedValue;
        }
    }
}
